import { plugin } from "alemon";
import data from '../../model/fantasyData.js';
import {
  __PATH,
  Add_HP,
  Read_player,
  existplayer,
  Write_equipment,
  Write_player,
  Read_bag,
  Write_bag,
  Read_mapevent,
  Judge_up,
  Add_bag,
} from '../../model/fantasy.js';
import { basename } from "path/win32";


export class UserStart extends plugin {
  constructor() {
    super({
      rule: [
                {
                    reg: /^你好$/,
                    fnc: 'nihao'
                },
                {
                    reg: /^注册.*$/,
                    fnc: 'zhuce'
                },{
                    reg: /^属性$/,
                    fnc: 'shuxing'
                },{
                    reg: /^信息$/,
                    fnc: 'xinxi'
                },{
                    reg: /^探索周边$/,
                    fnc: 'startTS'
                },{
                    reg: /^背包.*$/,
                    fnc: 'My_bag'
                },{
                    reg: /^地图信息$/,
                    fnc: 'Map_information'
                },

      ],
    });
    
  }
    //你好
    async nihao(e){
    e.reply("你好");
    return false;
    }


    //#注册
    async zhuce(e) {
        //有无存档
        let ifexistplay = await existplayer(e.user_id);
        if (ifexistplay) {
            e.reply("请不要重复注册哦~")
            return;
        }
        //初始化玩家信息
        let ran001 = 100 + Math.floor(800 * Math.random());
        let ran002 = 100 + Math.floor(800 * Math.random());
        let ran01 = Math.random();
        let ran02 = Math.random();
        let ran03 = Math.random();
        let ran04 = Math.random();
        let ran05 = Math.random();
        let ran06 = Math.random();
        let ran07 = ran01 + ran02 +ran03 + ran04 + ran05 + ran06;
        let ran1 = 1.5 * ran01 / ran07 + 0.5;
        let ran2 = 1.5 * ran02 / ran07 + 0.5;
        let ran3 = 1.5 * ran03 / ran07 + 0.5;
        let ran4 = 1.5 * ran04 / ran07 + 0.5;
        let ran5 = 1.5 * ran05 / ran07 + 0.5;
        let ran6 = 1.5 * ran06 / ran07 + 0.5;
        let talent = Math.floor(100 * Math.random())
        let name = e.cmd_msg.replace(/^注册/,'')
        if(name == "" || name == undefined){
        e.reply("请输入注册名称~")
        }
        let new_player = {
            "id": e.user_id,
            "sex": 0,//性别
            "昵称": name,
            "签名": "哎呀，还没有想好呢！",
            "等级": 0,
            "经验": 0,
            "位置": "幽静森林",
            "坐标x": ran001,
            "坐标y": ran002,
            "属性点":0,
            "天赋":talent,
            "体质潜力":ran1,
            "精神潜力":ran2,
            "力量潜力":ran3,
            "智力潜力":ran4,
            "敏捷潜力":ran5,
            "幸运潜力":ran6,
            "生命上限":0,
            "魔力上限":0,
            "当前生命":0,
            "当前魔力":0,
            "物攻":0,
            "魔攻":0,
            "物防":0,
            "魔防":0,
            "暴击":0,
            "暴伤":0,
            "命中":0,
            "闪避":0,
            "暴免":0,
            "暴抗":0,
            "韧性":0,
            "速度":0,
            "移速":0

        }
        await Write_player(e.user_id, new_player);


        //初始化装备
        let new_equipment = {
            "武器": "",
            "副手": "",
            "头肩": "",
            "上装": "",
            "腰带": "",
            "下装": "",
            "鞋子": "",
            "戒指": "",
            "手镯": "",
            "项链": "",
        }
        await Write_equipment(e.user_id, new_equipment);
        //初始化背包
        let new_bag = {
            "铜币": 10000,
            "银币": 0,
            "金币": 0,
            "装备": [],
            "道具": [],
            "技能书": [],
            "材料": [],
           
        }
        let player = await Read_player(e.user_id);
        await Write_bag(e.user_id, new_bag);
        await Add_HP(e.user_id, player.生命上限);
        e.reply(`一阵眩目的白光将你笼罩，逼得你睁不开眼。\n再次睁开眼时，你发觉周围是一片茂密的森林。`)
        e.reply(`[${player.昵称}]穿越成功！\n————————————\n【删档测试中———】\n发送[属性]查看角色属性\n发送[信息]查看角色信息\n发送[菜单]查看更多指令`)
        return;
    }
 
 
  //角色属性
  async shuxing(e) {
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    let img = (`【${player.昵称} Lv.${player.等级}】\n————————————\n生命: ${player.当前生命}\/${player.生命上限}\n魔力: ${player.当前魔力}\/${player.魔力上限}\n物理攻击：${player.物攻}\n魔法攻击：${player.魔攻}\n物理防御：${player.物防}\n魔法防御：${player.魔防}\n命中：${player.命中}\n闪避：${player.闪避}\n暴击：${player.暴击}\n暴伤：${player.暴伤}\n暴免：${player.暴免}\n暴抗：${player.暴抗}\n韧性：${player.韧性}\n速度：${player.速度}\n`)
    e.reply(img);
    return;
  }


  async xinxi(e){
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    let dengji = player.等级;
    let level = data.level_list;
    let level_max = level[dengji].经验;
    let talent = data.talent_list;
    let talent_word = "";
    let len = talent.length;
    for(let i = 0; i < len; i++){
        if(player.天赋 >= talent[i].数)
            talent_word = talent[i].字;
    }
    let img = (`【${player.昵称}】\n————————————\n等级：${player.等级}\n经验：${player.经验}\/${level_max}\n天赋：${talent_word}\n位置：${player.位置}\n坐标：\(${player.坐标x}，${player.坐标y}\)\n属性点：${player.属性点}`);
    e.reply(img);
    return;
}

async startTS(e){
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    const nowTime = new Date().getTime() // 获取当前日期的时间戳
    const restTime = nowTime - player.探索记录

    if(restTime < 600000){
       let minute = Math.floor((600000 - restTime) / 60000);
       let second = Math.floor((600000 - restTime - 60000 * minute) / 1000 + 1)
    e.reply(`【${player.昵称}】\n————————————\n您还在休息中哦~\n距离整装待发还差${minute}分${second}秒`)
    }else{


      let progress = player["进度"];
      if (progress === undefined) {
        player["进度"] = [];
      }
      player.探索记录 = nowTime;

      //搜索进度
      let name = player.位置;
      let value = -1;
      for (let i = 0; i < player["进度"].length; i++) {
        if (player["进度"][i].hasOwnProperty(name)) {
          value = player["进度"][i][name];
            if(value >= 100){
              e.reply(`【${player.昵称}】\n————————————\n该区域你已经探索完成啦！\n去做做其他的事吧~`);
              return;
            }
          player["进度"][i][name] += 1;
          break;
        }
      }
      if (value === -1) {
        let obj = {};
        obj[name] = 1;
        player["进度"].push(obj);
        value = 0;
      }

      //匹配进度相应探索
 let event = await Read_mapevent(player.位置);
     let i = -1;
     let indexes = event
       .map((obj, index) => obj["进度"] === value ? index : false)
       .filter(index => index !== false);
     if(indexes !== false){
       let len = Math.floor(Math.random() * indexes.length);
       if(len == 0){
       indexes = event
       .map((obj, index) => obj["进度"] === -1 ? index : false)
       .filter(index => index !== false);
       if(indexes !== false){
       len = Math.floor(Math.random() * indexes.length);
       i = indexes[len];
       }else{
       e.reply(`【系统错误】\n————————————\n未读取正确的探索值！`);
       }}else{
       i = indexes[len];
       }
     }else{
       e.reply(`【系统错误】\n————————————\n未读取正确的探索值！`);
     }
     //读取相应数据
     let EXP = Math.floor(0.8 * event[i]["经验"] + 0.4 * event[i]["经验"] * Math.random());
     let msg1 = "";
     let msg2 = "";
     let msg0 = "";
     player.经验 += EXP;
     if(EXP > 0){
     msg1 = `\n★获得${EXP}经验★`
     }else if(EXP < 0){
     msg1 = `\n★损失${EXP}经验★`
     }else{
     msg1 = "";
     }
     if(event[i].数量 > 0){
       await Add_bag(usr_qq, event[i].物品, event[i].数量)
     msg2 = `\n★获得[${event[i]["物品"]}] * ${event[i]["数量"]}★`
     }
     let event_word = event[i]["描述"];
     if(msg1 !== "" || msg2 !== "" ){
       msg0 = "\n————————————";
     }
     e.reply(`【${player.昵称}】\n${event_word}` + msg0 + msg1 + msg2);
     await Write_player(usr_qq, player);
     await Judge_up(usr_qq, e);
     return;
}

}


async My_bag(e){
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    let name0 = e.cmd_msg.replace(/^背包/,'');
    let name = name0.replace(/\d+/g, "");
    let match = name0.match(/\d+$/);
    let num = match ? match[0] : 1;
    const bag = await Read_bag(usr_qq);
    if((name == "装备")||(name == "材料")||(name == "道具")||(name == "技能书")){
      let len = bag[name].length;
      let len1 = Math.floor((len - 1)/ 10) + 1;
      let word = `【[${player.昵称}]的${name}背包】\n————————————`;
      if(len == 0){
        e.reply(`【[${player.昵称}]的${name}背包】\n————————————\n[空空如也]\n————————————\n【第1页，共1页】\nTips:发送[背包+类别+页码]查看背包`)
      }
      let word0 = "";
      let word1 = "";
      let i = 10 * (num - 1);
      let b = i + 10;
      if(num == len1){
        b = len - 10 * (num - 1);
      }
      for(i; i < b; i++){
      word0 = `[${bag[name][i]["名称"]}]`;
      word1 = `${bag[name][i]["数量"]}`;
      if(word1 == "" || word1 == undefined){
        word = 1;
      }
      if(word0 !== "" && word0 !== undefined){
        word = word + `\n ${word0} * ${word1}`;
      }
      }
      e.reply(word + `\n————————————\n【第${num}页，共${len1}页】\nTips:发送[背包+类别+页码]查看背包`)
    }else{
      e.reply(`查看类别不对哦~\n不存在类别[${name}]`)
    }
}

  async Map_information(e){
    let usr_qq = e.user_id;
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let player = await Read_player(usr_qq);
    let local = player["位置"];
    let jindu = (player && player["进度"] && player["进度"][local]) || 0;
    let jindu_word = "";
    if(jindu >= 0 && jindu < 100){
      const nowTime = new Date().getTime() // 获取当前日期的时间戳
      const restTime = nowTime - player.探索记录
      if(restTime <= 600000 && restTime >= 0){
       let minute = Math.floor((600000 - restTime) / 60000);
       let second = Math.floor((600000 - restTime - 60000 * minute) / 1000 + 1)
          jindu_word = `\n☆已探索：${jindu}%☆\n[距离下一次探索${minute}分${second}秒]`;
        }else{
          jindu_word = `\n☆已探索：${jindu}%☆\n[已经可以下一次探索了！]`;
        }
    }else if(jindu >= 100){
      jindu_word = `\n★探索已完成★`
    }else{
      const nowTime = new Date().getTime() // 获取当前日期的时间戳
      const restTime = nowTime - player.探索记录
      if(restTime <= 600000 && restTime >= 0){
       let minute = Math.floor((600000 - restTime) / 60000);
       let second = Math.floor((600000 - restTime - 60000 * minute) / 1000 + 1);
          jindu_word = `\n☆已探索：${jindu}%☆\n[距离下一次探索${minute}分${second}秒]`;
        }else{
          jindu_word = `\n☆已探索：${jindu}%☆\n[已经可以下一次探索了！]`;
        }
    }
    let img = `【${player.昵称}】\n————————————\n所在区域：[${local}]` + jindu_word;
    img = img + `\n————————————\n【???无法查阅更多信息???】`;
    e.reply(img);
    return;
    }  











}
